package com.example.yichuangyun.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.yichuangyun.R;
import com.example.yichuangyun.databinding.SetShareItemBinding;
import com.example.yichuangyun.utils.treelist.Node;
import com.example.yichuangyun.utils.treelist.TreeRecyclerAdapter;

import java.util.List;

public class SetShareAdapter extends TreeRecyclerAdapter {

    private OnCheckListener onCheckListener;
    private OnSingleCheckListener onOnSingleCheckListener;
    private boolean isSingle;
    /**
     *  0订单签单人 1订单维护人 2订单添加跟进
     */
    private int flag;

    public SetShareAdapter(Context context, List<Node> data, int defaultExpandLevel) {
        super(context, data, defaultExpandLevel);
    }

    public SetShareAdapter(Context context, List<Node> data, int defaultExpandLevel, boolean isSingle) {
        super(context, data, defaultExpandLevel);
        this.isSingle = isSingle;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        SetShareItemBinding itemBinding = SetShareItemBinding.inflate(LayoutInflater.from(mContext), parent, false);
        return new MyHolder(itemBinding);
    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    public void onBindViewHolder(final Node node, RecyclerView.ViewHolder holder, int position) {
        final MyHolder viewHolder = (MyHolder) holder;
        viewHolder.binding.rlItem.setOnClickListener(v -> {
            // 设置共享人是多选，
            if (node.getIsDept() == 1 && !isSingle) {
                expandOrCollapse(position);
            } else {
                if (isSingle) {
                    // 订单维护人
                    if (flag == 1 && node.getIsDept() == 1) {
                        expandOrCollapse(position);
                    } else {
                        if (node.isChecked()) {
                            node.setChecked(false);
                        } else {
                            for (Node node1 : mAllNodes) {
                                node1.setChecked(false);
                            }
                            node.setChecked(true);
                        }
                        notifyDataSetChanged();
                        if (onOnSingleCheckListener != null) {
                            onOnSingleCheckListener.onClick(node);
                        }
                    }
                } else {
                    node.setChecked(!node.isChecked());
                    setChecked(node, node.isChecked());
                    if (onCheckListener != null) {
                        onCheckListener.onClick(node);
                    }
                }
            }
        });

        viewHolder.binding.rlItem.setBackgroundColor(node.isChecked() ? 0xFFDAE3FF : 0xFFFFFFFF);
        viewHolder.binding.ivSelectTree.setImageResource(node.isChecked() ? R.mipmap.icon_choose_square : R.mipmap.icon_nochoose_square);
        viewHolder.binding.ivSelectTree.setVisibility(node.getIsDept() == 1 ? View.GONE : View.VISIBLE);
        viewHolder.binding.icon.setOnClickListener(view -> expandOrCollapse(position));
        if (node.getIcon() == -1) {
            viewHolder.binding.icon.setVisibility(node.getIcon() == -1 ? View.GONE : View.VISIBLE);
        } else {
            viewHolder.binding.icon.setVisibility(View.VISIBLE);
            viewHolder.binding.icon.setImageResource(node.getIcon());
        }
        viewHolder.binding.tvTreeNodeLabel.setText(node.getName());
    }

    public void setFlag(int flag) {
        this.flag = flag;
        notifyDataSetChanged();
    }

    static class MyHolder extends RecyclerView.ViewHolder {

        SetShareItemBinding binding;

        public MyHolder(SetShareItemBinding itemBinding) {
            super(itemBinding.getRoot());
            binding = itemBinding;
        }
    }

    public interface OnCheckListener {
        void onClick(Node node);
    }

    public interface OnSingleCheckListener {
        void onClick(Node node);
    }

    public void setOnOnCheckListener(OnCheckListener onCheckListener) {
        this.onCheckListener = onCheckListener;
    }

    public void setOnOnSingleCheckListener(OnSingleCheckListener onOnSingleCheckListener) {
        this.onOnSingleCheckListener = onOnSingleCheckListener;
    }
}
